package Comprehensive_04;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test3 {
    /*
        abc循环输出十次 利用三个线程 输出:abc abc abc...
     */
    public static void main(String[] args) {
        MyThread t1 = new MyThread("a");
        MyThread t2 = new MyThread("b");
        MyThread t3 = new MyThread("c");

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
    }

}

class MyThread implements Runnable{
    private String s;

    Lock lock = new ReentrantLock();
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();

    CountDownLatch latch = new CountDownLatch(1);

    public MyThread() {
    }

    public MyThread(String s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        lock.lock();
        try {
            printABC(s);
            c1.signal();
            c2.signal();
            c3.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void printABC(String s) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            System.out.print(s);
        }
    }

    /**
     * 获取
     * @return s
     */
    public String getS() {
        return s;
    }

    /**
     * 设置
     * @param s
     */
    public void setS(String s) {
        this.s = s;
    }

    public String toString() {
        return "MyThread{s = " + s + "}";
    }
}
